package com.itic.web.mtype.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.persistence.model.ModelFiles;
import com.itic.demo.modelfiles.service.ModelFilesService;
import com.itic.ht.actcms.notice.persistence.model.WebNotice;
import com.itic.ht.actcms.notice.persistence.model.WebNoticeExample;
import com.itic.ht.actcms.notice.service.WebNoticeService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.mtype.persistence.model.WebmType;
import com.itic.ht.mem.mtype.persistence.model.WebmTypeExample;
import com.itic.ht.mem.mtype.service.WebmTypeService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.DataResult;
import com.itic.web.ro.Datas;
import com.itic.web.util.TokenUtil;

@Service
@Transactional
public class AppMemTypeService {
	

	@Autowired
	private WebMemberService webMemberService;
    @Autowired
    private ModelFilesService modelFilesService;
    @Autowired
    private WebmTypeService memTypeService;
    @Autowired
    private WebNoticeService webNoticeService;
	
	public DataResult getTypeList(TokenData data){
		
		WebMember member = null;
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return DataResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return DataResult.error(2, "token失效");
			}
	        
			if(StringHelper.isEmpty(data.getToken())){
			}
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("查询到的用户帐号是"+member.getAccount());
			
			//查询通告详情
			WebNotice webNotice = null;
			WebNoticeExample noticeExample = new WebNoticeExample();
			noticeExample.createCriteria().andCategoryEqualTo("001");
			List<WebNotice> noticeList = webNoticeService.list(noticeExample);
			if(noticeList!=null && noticeList.size()>0){
				System.out.println("1111111111111111111111111111111");
				webNotice = noticeList.get(0);
				System.out.println("22222222222222222222222222");
	         	String webNoticefileId = webNotice.getFileId();
	         	System.out.println("333333333333333333333333333");
	         	if(webNoticefileId!=null && !"".equals(webNoticefileId)){
	         		System.out.println("4444444444444444444444444444");
	         		ModelFiles webNoticeFile = modelFilesService.getFile(webNoticefileId);
	         		System.out.println("5555555555555555555555555555555");
	    		     if(webNoticeFile!=null){
	    		    	 System.out.println("6666666666666666666666666666");
	    		    	 webNotice.setFileId(webNoticeFile.getFilePath());
	    		     }
					
				}
			}
			
			// 获取数据
			WebmTypeExample sypeExample = new WebmTypeExample();
			sypeExample.setOrderByClause("REORDER ASC");
			sypeExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
	        
			List<WebmType> typelist = memTypeService.list(sypeExample);
            for (WebmType webVedio : typelist) {
            	String fileId = webVedio.getFileId();
            	if(fileId != null && !"".equals(fileId)){
            		webVedio.setFileId(modelFilesService.getFile(fileId).getFilePath());
            	}
			}
            
            Datas datas = new Datas();
            datas.setMemdata(webNotice);
            datas.setGlobaldatas(typelist);
            
			return DataResult.build(200, "会员卡列表查询成功",datas);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return DataResult.error(4, "会员卡列表查询失败");
		}

	}

}
